package org.linlinjava.litemall.wx.web;

import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.dto.PayRequest;
import org.linlinjava.litemall.wx.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/wx/pay")
public class PayController {

    @Autowired
    private PayService payService;

    /**
     * 预支付
     * @param userId
     * @param body
     * @param request
     * @return
     */
    @PostMapping("prepay")
    public Object prepay(@LoginUser Integer userId, @RequestBody PayRequest body, HttpServletRequest request) {
        if (body.getAttach()==null) {
            return ResponseUtil.badArgument();
        }
        return payService.prepay(userId, body, request);
    }

    /**
     * 微信付款成功或失败回调接口
     * <p>
     *  注意，这里pay-notify是示例地址，建议开发者应该设立一个隐蔽的回调地址
     *
     * @param request 请求内容
     * @param response 响应内容
     * @return 操作结果
     */
    @PostMapping("pay-notify")
    public Object payNotify(HttpServletRequest request, HttpServletResponse response) {
        return payService.payNotify(request, response);
    }
}
